﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class FormMain
    Dim timerTaskStep As Timer '任务步骤
    Dim timerTaskStep_interval As Integer = 3000 '时间间隔

    Dim objClsSysParam As New clsSysParam

    Dim objagvlog As New clsAgvLog

    Dim agv_website As String

    Dim dtTaskStep As DataTable

    Dim dtTaskStep1 As DataTable

    Dim httphelper As New HttpHelper

    Dim objClsTaskStep As New clsTaskStep

    Dim dgvSelectedTaskStepSysId As String = "-1"
    Dim dgvSelectedTaskStepTaskId As String = "0"
    Dim dgvSelectedTaskStepProdSpecId As String = String.Empty

    Dim dgvSelectedTaskStepSysId1 As String = "-1"
    Dim dgvSelectedTaskStepTaskId1 As String = "0"
    Dim dgvSelectedTaskStepProdSpecId1 As String = String.Empty
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        initAgvWebSite()
        initTimer()
        initTaskStep(cbSingle.Checked)
        initTaskStep1(cbSingle.Checked)
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_AGV_Load.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub

    Private Sub initAgvWebSite()
        Dim dt As DataTable = objClsSysParam.selectAgvWebSite()
        If dt.Rows.Count = 1 Then
            agv_website = dt.Rows(0)("param_value").ToString()
        End If
    End Sub
    Private Sub initTimer()
        timerTaskStep = New Timer
        timerTaskStep.Interval = timerTaskStep_interval
        AddHandler timerTaskStep.Tick, AddressOf timerTaskStep_Tick
        timerTaskStep.Start()
    End Sub

    Private Sub timerTaskStep_Tick(sender As Object, e As EventArgs)
        Try
            initTaskStep(cbSingle.Checked)
            initTaskStep1(cbSingle.Checked)
        Catch ex As Exception
            timerTaskStep.Stop()
            MsgBox("与服务器断开连接，请检查网络状态。")
            End 'Application.Restart()
        End Try
    End Sub

    Private Sub initTaskStep(ty As Boolean)
        '监听任务步骤表的数据变化
        If rb500_700.Checked Then
            dtTaskStep = objClsTaskStep.SelectTaskStep1("待创建1", "'入炉','出入炉'", "135", ty)
        Else
            dtTaskStep = objClsTaskStep.SelectTaskStep1("待创建1", "'入炉','出入炉'", "136", ty)
        End If
        dgvTaskStep.AutoGenerateColumns = False
        dgvTaskStep.DataSource = dtTaskStep
        For index = 0 To dgvTaskStep.Rows.Count - 1
            dgvTaskStep.Rows(index).Selected = False
        Next
        For index = 0 To dgvTaskStep.Rows.Count - 1
            If dgvTaskStep.Rows(index).Cells("Col_sys_id").Value.ToString() = dgvSelectedTaskStepSysId Then
                dgvTaskStep.Rows(index).Selected = True
                Exit For
            End If
        Next
    End Sub

    Private Sub initTaskStep1(ty As Boolean)
        '监听任务步骤表的数据变化
        If rb500_700.Checked Then
            dtTaskStep1 = objClsTaskStep.SelectTaskStep1("待创建1", "'出炉'", "135", ty)
        Else
            dtTaskStep1 = objClsTaskStep.SelectTaskStep1("待创建1", "'出炉'", "136", ty)
        End If
        dgvTaskStep1.AutoGenerateColumns = False
        dgvTaskStep1.DataSource = dtTaskStep1
        For index = 0 To dgvTaskStep1.Rows.Count - 1
            dgvTaskStep1.Rows(index).Selected = False
        Next
        For index = 0 To dgvTaskStep1.Rows.Count - 1
            If dgvTaskStep1.Rows(index).Cells("Colsys_id").Value.ToString() = dgvSelectedTaskStepSysId1 Then
                dgvTaskStep1.Rows(index).Selected = True
                Exit For
            End If
        Next
    End Sub

    Private Sub dgvTaskStep_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvTaskStep.MouseClick
        For index = 0 To dgvTaskStep1.Rows.Count - 1
            dgvTaskStep1.Rows(index).Selected = False
        Next
        dgvSelectedTaskStepSysId1 = "-1"
        dgvSelectedTaskStepTaskId1 = "0"
        dgvSelectedTaskStepProdSpecId1 = String.Empty
        If dgvTaskStep.Rows.Count > 0 Then
            dgvSelectedTaskStepSysId = dgvTaskStep.Rows(dgvTaskStep.CurrentRow.Index).Cells("Col_sys_id").Value.ToString()
            dgvSelectedTaskStepTaskId = dgvTaskStep.Rows(dgvTaskStep.CurrentRow.Index).Cells("Col_task_id").Value.ToString()
            dgvSelectedTaskStepProdSpecId = dgvTaskStep.Rows(dgvTaskStep.CurrentRow.Index).Cells("Col_prod_spec_id").Value.ToString()
        End If
    End Sub

    Private Sub dgvTaskStep1_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvTaskStep1.MouseClick
        For index = 0 To dgvTaskStep.Rows.Count - 1
            dgvTaskStep.Rows(index).Selected = False
        Next
        dgvSelectedTaskStepSysId = "-1"
        dgvSelectedTaskStepTaskId = "0"
        dgvSelectedTaskStepProdSpecId = String.Empty
        If dgvTaskStep1.Rows.Count > 0 Then
            dgvSelectedTaskStepSysId1 = dgvTaskStep1.Rows(dgvTaskStep1.CurrentRow.Index).Cells("Colsys_id").Value.ToString()
            dgvSelectedTaskStepTaskId1 = dgvTaskStep1.Rows(dgvTaskStep1.CurrentRow.Index).Cells("Coltask_id").Value.ToString()
            dgvSelectedTaskStepProdSpecId1 = dgvTaskStep1.Rows(dgvTaskStep1.CurrentRow.Index).Cells("Colprod_spec_id").Value.ToString()

        End If
    End Sub

    Private Sub rb500_700_CheckedChanged(sender As Object, e As EventArgs) Handles rb500_700.CheckedChanged
        If rb500_700.Checked Then
            initTaskStep(cbSingle.Checked)
            initTaskStep1(cbSingle.Checked)
        End If
    End Sub

    Private Sub rb800_1000_CheckedChanged(sender As Object, e As EventArgs) Handles rb800_1000.CheckedChanged
        If rb800_1000.Checked Then
            initTaskStep(cbSingle.Checked)
            initTaskStep1(cbSingle.Checked)
        End If
    End Sub
    Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
        Dim agvselect As String
        If String.IsNullOrEmpty(cmbAgv.Text) Then
            agvselect = "0"
        Else
            agvselect = cmbAgv.Text
        End If
        If dgvSelectedTaskStepSysId.Equals("-1") = False And dgvSelectedTaskStepSysId1.Equals("-1") = True Then
            'If String.IsNullOrEmpty(dgvSelectedTaskStepProdSpecId) = False Then
            Dim msg As String = setAgvNewTask(dgvSelectedTaskStepSysId,
                                          dgvTaskStep.SelectedRows(0).Cells("Col_prio").Value.ToString(),
                                          dgvTaskStep.SelectedRows(0).Cells("Col_pick").Value.ToString(),
                                          dgvTaskStep.SelectedRows(0).Cells("Col_drop").Value.ToString(),
                                          agvselect)
            If msg.Equals("ok") Then
                'agv的操作日志
                Dim localip As String = GetIPAddr()
                objagvlog.add(localip, agvselect, dgvSelectedTaskStepSysId, "上料")
                '更新status 状态为ok
                Dim rtnu As Integer = objClsTaskStep.updateTaskStep(agvselect, "已创建1", "已创建1", dgvSelectedTaskStepSysId, dgvSelectedTaskStepTaskId, dgvSelectedTaskStepProdSpecId)
                If rtnu <> -2 Then
                    dgvSelectedTaskStepSysId = "-1"
                    dgvSelectedTaskStepTaskId = "0"
                    dgvSelectedTaskStepProdSpecId = String.Empty
                    dgvSelectedTaskStepSysId1 = "-1"
                    dgvSelectedTaskStepTaskId1 = "0"
                    dgvSelectedTaskStepProdSpecId1 = String.Empty
                    cmbAgv.Text = String.Empty
                    MsgBox("操作成功")
                Else
                    MsgBox("操作失败")
                End If
            Else
                MsgBox("失败信息：" & msg & "，请确保AGV在上料点位置")
            End If
            'Else
            'MsgBox("未获取到待上料的支座型号")
            'End If
        ElseIf dgvSelectedTaskStepSysId.Equals("-1") = True And dgvSelectedTaskStepSysId1.Equals("-1") = False Then

            Dim msg As String = setAgvNewTask(dgvSelectedTaskStepSysId1,
                                      dgvTaskStep1.SelectedRows(0).Cells("Colprio").Value.ToString(),
                                      dgvTaskStep1.SelectedRows(0).Cells("Colpick").Value.ToString(),
                                      dgvTaskStep1.SelectedRows(0).Cells("Coldrop").Value.ToString(),
                                      agvselect)
            If msg.Equals("ok") Then
                '更新status 状态为ok
                Dim localip As String = GetIPAddr()
                objagvlog.add(localip, agvselect, dgvSelectedTaskStepSysId1, "上料")

                Dim rtnu As Integer = objClsTaskStep.updateTaskStep(agvselect, "已创建1", "已创建1", dgvSelectedTaskStepSysId1, dgvSelectedTaskStepTaskId1, dgvSelectedTaskStepProdSpecId1)
                If rtnu <> -2 Then
                    dgvSelectedTaskStepSysId = "-1"
                    dgvSelectedTaskStepTaskId = "0"
                    dgvSelectedTaskStepProdSpecId = String.Empty
                    dgvSelectedTaskStepSysId1 = "-1"
                    dgvSelectedTaskStepTaskId1 = "0"
                    dgvSelectedTaskStepProdSpecId1 = String.Empty
                    cmbAgv.Text = String.Empty
                    MsgBox("操作成功")
                Else
                    MsgBox("操作失败")
                End If
            Else
                MsgBox("失败信息：" & msg)
            End If
        Else
            MsgBox("请选择硫化机待上料任务")
        End If
    End Sub

    Private Function setAgvNewTask(id As String, prio As String, pick As String, drop As String, agv As String) As String
        Dim msg As String
        If String.IsNullOrEmpty(agv_website) = False Then
            Dim url As String = agv_website & "AGVAPI?do=NewTask&id=" & id & "&prio=" & prio & "&pick=" & pick & "&drop=" & drop & "&agv=" & agv
            Dim httphelper As HttpHelper = New HttpHelper()
            Dim rtn As String = httphelper.HttpGet(url, "")
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            msg = jo("msg").ToString() '输出ok
        Else
            msg = "请联系工作人员，配置agv网址"
        End If
        Return msg
    End Function

    Private Sub FormMain_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        timerTaskStep.Stop()
        timerTaskStep.Enabled = False
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

    Private Sub 任务列表ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 任务列表ToolStripMenuItem.Click
        FormTaskList.ShowDialog()
    End Sub
End Class